package com.terracloud.server.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 用于在设备添加、更新、删除后同步网关路由配置
 *
 * @author chenhainan
 * @since 2025/11/13
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GatewayRouteSyncUtils {

    private final RestTemplate restTemplate;

    @Value("${gateway.refresh-route-url}")
    private String gatewayRefreshRoutUrl;

    /**
     * 同步路由信息,注意此方法为异步执行
     */
    public void syncRoute() {
        Thread.startVirtualThread(() -> {
            final var syncResult = restTemplate.getForObject(gatewayRefreshRoutUrl, Boolean.class);
            if (log.isDebugEnabled()) {
                log.debug("请求[{}]同步设备路由信息,结果为[{}]", gatewayRefreshRoutUrl, syncResult);
            }
        });
    }

}
